package com.cckj.common.response;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@ToString
public class ResultData<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    private static final int ERROR_CODE = 500;
    private static final int SUCCESS_CODE = 200;
    private static final String SUCCESS_MSG = "操作成功！";
    private static final String FAILED_MSG = "操作失败！";

    public static final String ERROR_MSG = "系统繁忙！";

    @Getter
    private final String PATTERN = "yyyy-MM-dd HH:mm:ss";
    @Getter
    @Setter
    private String timestamp = LocalDateTime.now().format(DateTimeFormatter.ofPattern(PATTERN));
    @Getter
    @Setter
    private String message = "操作成功！";
    @Getter
    @Setter
    private Integer status = 200;
    @Getter
    @Setter
    private T data;

    public static ResultData<Object> error() {
        return error(ERROR_CODE, FAILED_MSG, null);
    }

    public static ResultData<Object> error(String message) {
        return error(ERROR_CODE, message, null);
    }

    public static ResultData<Object> error(int code, String message) {
        return error(code, message, null);
    }

    public static ResultData<Object> error(Object result) {
        return error(ERROR_CODE, FAILED_MSG, result);
    }

    public static ResultData<Object> error(int code, String message, Object result) {
        ResultData<Object> r = new ResultData<>();
        r.setStatus(code);
        r.setMessage(message);
        r.setData(result);
        return r;
    }

    public static ResultData<Object> ok() {
        return ok(SUCCESS_CODE, SUCCESS_MSG, null);
    }

    public static ResultData<Object> ok(String message) {
        return ok(SUCCESS_CODE, message, null);
    }

    public static ResultData<Object> ok(Object result) {
        return ok(SUCCESS_CODE, SUCCESS_MSG, result);
    }

    public static ResultData<Object> ok(int code, String message, Object result) {
        ResultData<Object> r = new ResultData<>();
        r.setMessage(message);
        r.setStatus(code);
        r.setData(result);
        return r;
    }

}